package view;

import controller.SystemController;
import model.Snippet;
import vandorenmes.argusv3.R;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class ScannerActivity extends Activity 
{
	public static final String TAG = ScannerActivity.class.getSimpleName();
	
	private SystemController _controller;
	private SurfaceHolder _holder;
	
	private CameraView _cameraView;
	
	private Handler _toastHandler;
	private static Toast _toastje;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		Log.d(TAG, String.format("onCreate()"));
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_scanner);
		
		this._toastHandler = new Handler();
		_toastje = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
		
		_controller = new SystemController(this);
		
		_cameraView = (CameraView) findViewById(R.id.camera_preview);
	}
	
	public synchronized void makeToast(final String msg)
	{
		_toastHandler.post(new Runnable() {
			@Override
			public void run() {	
				_toastje.cancel();	// cancel previous toast			
				
				_toastje = Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT);
				_toastje.show();
			}
		});
		
	}

	public void addSnippetView(Snippet newSnippet) 
	{
		Log.d(TAG, String.format("AddSnippetView() - %s", newSnippet.toString()));
		
		RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);		
		SnippetView view = new SnippetView(this, newSnippet);
		
		mainLayout.addView(view);		
		newSnippet.addObserver(view);
//		newSnippet.notifyObservers();
	}

	public void startCamera(SurfaceHolder surfaceHolder)
	{
		Log.d(TAG, String.format("startCamera()"));
		_holder = surfaceHolder;
		if(_controller == null)
			_controller = new SystemController(this);
		_controller.startCamera(surfaceHolder);
	}
	
	public void removeSnippetFromView(SnippetView snipView)
	{
		RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);	
		mainLayout.removeView(snipView);
	}
	
	@Override
	protected void onResume() 
	{
		Log.d(TAG, String.format("onResume()"));
		super.onResume();
		if(_controller == null)
			_controller = new SystemController(this);
		_controller.resumeCamera(_holder);
	}
	
	@Override
    protected void onPause() 
	{
		Log.d(TAG, String.format("onPause()"));
	    super.onPause();
	     
	    RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main_layout);
	    mainLayout.removeViewsInLayout(1, mainLayout.getChildCount()-1);
	    	    
	    _controller.stopCamera();
	}
	
	@Override
	protected void onStop() 
	{
		Log.d(TAG, String.format("onStop()"));
		super.onStop();
		onPause();
	}
	
	public void drawPoints(float[] points)
	{
		_cameraView.setPoints(points);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) 
	{		
		Log.d(TAG, "TOUCH!");
		
		if(event.getAction() == MotionEvent.ACTION_DOWN)
		{
			//Camera has flash
			if(getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH))
			{
				Log.d(TAG, "TOUCH! - FLASH!");
				_controller.toggleFlashLight();
			}
			return super.onTouchEvent(event);
		}
		
		return false;
	}
}
